package com.qidian.hw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.qidian.hw.dao.CaseInfoDAO;
import com.qidian.hw.pojo.CaseInfo;
import com.qidian.hw.tools.FormatUtil;

/**
* @author 郭浩
*
*@company 起点编程
*
* 2022年8月19日 上午8:54:23
*/
public class CaseInfoDAOImpl extends BaseDAO implements CaseInfoDAO{
	private Connection con;
	private PreparedStatement pstmt;
	private ResultSet rs;
	public ArrayList<CaseInfo> caseInfoList() {
		ArrayList<CaseInfo> al = new ArrayList<CaseInfo>();
		String sql = "select * from case_info where status = 1";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				CaseInfo caseInfo = new CaseInfo();
				caseInfo.setCaseId(rs.getLong("case_id"));
				caseInfo.setCaseTitle(rs.getString("case_title"));
				caseInfo.setCaseDesc(rs.getString("case_desc"));
				caseInfo.setCaseImg(rs.getString("case_img"));
				caseInfo.setCaseDetails(rs.getString("case_details"));
				caseInfo.setCaseDetailsImg(rs.getString("case_details_img"));
				caseInfo.setCreateTime(rs.getString("create_time"));
				caseInfo.setModifyTime(rs.getString("modify_time"));
				al.add(caseInfo);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return al;
	}

	public CaseInfo queryCaseInfoById(long id) {
		CaseInfo caseInfo = null;
		String sql = "select * from case_info where case_id = ?";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setLong(1, id);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				caseInfo = new CaseInfo();
				caseInfo.setCaseId(rs.getLong("case_id"));
				caseInfo.setCaseTitle(rs.getString("case_title"));
				caseInfo.setCaseDesc(rs.getString("case_desc"));
				caseInfo.setCaseImg(rs.getString("case_img"));
				caseInfo.setCaseDetails(rs.getString("case_details"));
				caseInfo.setCaseDetailsImg(rs.getString("case_details_img"));
				caseInfo.setCreateTime(rs.getString("create_time"));
				caseInfo.setModifyTime(rs.getString("modify_time"));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return caseInfo;
	}

	public int caseInfoUpdate(CaseInfo caseInfo) {
		int result = 0;
		String sql = "update case_info set case_title = ?,case_desc = ?,case_img = ?,case_details = ?,case_details_img = ?,modify_time = ? where case_id = ?";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, caseInfo.getCaseTitle());
			pstmt.setString(2, caseInfo.getCaseDesc());
			pstmt.setString(3, caseInfo.getCaseImg());
			pstmt.setString(4, caseInfo.getCaseDetails());
			pstmt.setString(5, caseInfo.getCaseDetailsImg());
			String date = FormatUtil.getNow();
			pstmt.setString(6, date);
			pstmt.setLong(7, caseInfo.getCaseId());
			result = pstmt.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}

	public int caseInfoDelete(long id) {
		int result = 0;
		String sql = "update case_info set modify_time = ?,status = 0 where case_id = ?";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			String date = FormatUtil.getNow();
			pstmt.setString(1, date);
			pstmt.setLong(2,id);
			result = pstmt.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}

	public int caseInfoSave(CaseInfo caseInfo) {
		int result = 0;
		String sql = "insert into case_info(case_title,case_desc,case_img,case_details,case_details_img,create_time,modify_time) values(?,?,?,?,?,?,?)";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, caseInfo.getCaseTitle());
			pstmt.setString(2, caseInfo.getCaseDesc());
			pstmt.setString(3, caseInfo.getCaseImg());
			pstmt.setString(4, caseInfo.getCaseDetails());
			pstmt.setString(5, caseInfo.getCaseDetailsImg());
			String date = FormatUtil.getNow();
			pstmt.setString(6, date);
			pstmt.setString(7, date);
			result = pstmt.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}

	public int count(String title) {
		int result = 0;
		String sql = "select count(*) from case_info where status = 1";
		if(FormatUtil.isNull(title)) {
			sql += "and case_title like'%"+title+"%'";
		}
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				result = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}

	public ArrayList<CaseInfo> queryByPage(int page, int size, String title) {
		ArrayList<CaseInfo> caseInfos = new ArrayList<CaseInfo>();
		String sql = "select * from case_info where status = 1 ";
		// 拼接条件
		if (FormatUtil.isNull(title)) {
			sql += " and case_title like '%" + title + "%'";
		}
		sql += " limit "+((page-1)*size)+","+size;
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				CaseInfo caseInfo = new CaseInfo();
				caseInfo.setCaseId(rs.getLong("case_id"));
				caseInfo.setCaseTitle(rs.getString("case_title"));
				caseInfo.setCaseDesc(rs.getString("case_desc"));
				caseInfo.setCaseImg(rs.getString("case_img"));
				caseInfo.setCaseDetails(rs.getString("case_details"));
				caseInfo.setCaseDetailsImg(rs.getString("case_details_img"));
				caseInfo.setCreateTime(rs.getString("create_time"));
				caseInfo.setModifyTime(rs.getString("modify_time"));
				caseInfos.add(caseInfo);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return caseInfos;
	}

	public int deletes(String[] ids) {
		int result = 0;
		String sql = "update case_info set status = 0 where case_id in(";
		for(int i = 0;i < ids.length;i ++) {
			sql += ids[i] + ",";
		}
		sql += "0)";
		try {
			con = getCon();
			pstmt = con.prepareStatement(sql);
			result = pstmt.executeUpdate();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(rs, pstmt, con);
		}
		return result;
	}

}
